
var list;

$(function () {
    $("#menuId").val(sessionStorage.menuId);
    findRoleList();
});



//查询菜单和对应权限
function findRoleList(){
    var menuId = sessionStorage.rmId;


    var state = $("#s3").val();
    var params = {
        "type":"query",
        "menuId":menuId,
        "roleId":sessionStorage.roleId
    };
    $.ajax({
        type:"post",
        url: "/srole/findPermissions",
        data:params,
        dataType: "json",
        success : function(data){
            // console.log(data);
            if(data.code == 1 && data.code != undefined){
                list = data.menus;
                var html = "<table style='width: 98%;'>";
                for(var b in list){
                    var ls = list[b].list;
                    // console.log(ls);
                    if(list[b].menuPermissions == 1){
                        html += '<tr><td pid="'+list[b].id+'"  style="font-size: 20px;width: 150px;padding: 5px 5px;border-bottom: 1px solid #e2e2e2;"><input name="pmenu" pid="'+list[b].id+'" type="checkbox" checked="checked"/><label onclick="ck('+b+')" style="cursor: pointer;font-size: 18px;color: #435e75">'+list[b].permissionName+'</label></td></tr>';
                    }else {
                        html += '<tr><td pid="'+list[b].id+'"  style="font-size: 20px;width: 150px;padding: 5px 5px;border-bottom: 1px solid #e2e2e2;"><input name="pmenu" pid="'+list[b].id+'" type="checkbox"/><label onclick="ck('+b+')" style="cursor: pointer;font-size: 18px;color: #435e75">'+list[b].permissionName+'</label></td></tr>';
                    }

                    for(var j in ls){
                        if(ls[j].menuPermissions == 1){
                            html += '<tr name="tt'+b+'" style="display: none;"><td style="padding: 10px 10px;width: 180px;"><label style="cursor: pointer;"><input onchange="iseFfective(this,'+ls[j].id+')" name="cmenu" cid="'+ls[j].id+'" type="checkbox" checked="checked"/>'+ls[j].permissionName+'</label></td>';

                            if(ls[j].query == 1){
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="q'+ls[j].id+'" name="queryp" qmenuId="'+ls[j].id+'" type="checkbox" checked="checked"/>查看</label></td>'
                            }else {
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="q'+ls[j].id+'" name="queryp" qmenuId="'+ls[j].id+'" type="checkbox"/>查看</label></td>'
                            }
                            if(ls[j].add == 1){
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="a'+ls[j].id+'" name="addp" amenuId="'+ls[j].id+'" type="checkbox" checked="checked"/>添加</label></td>'
                            }else {
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="a'+ls[j].id+'" name="addp" amenuId="'+ls[j].id+'" type="checkbox"/>添加</label></td>'
                            }
                            if(ls[j].del == 1){
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="d'+ls[j].id+'" name="delp" dmenuId="'+ls[j].id+'" type="checkbox" checked="checked"/>删除</label></td>'
                            }else {
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="d'+ls[j].id+'" name="delp" dmenuId="'+ls[j].id+'" type="checkbox"/>删除</label></td>'
                            }
                            if(ls[j].edit == 1){
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="e'+ls[j].id+'" name="editp" emenuId="'+ls[j].id+'" type="checkbox" checked="checked"/>修改</label></td>'
                            }else {
                                html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="e'+ls[j].id+'" name="editp" emenuId="'+ls[j].id+'" type="checkbox"/>修改</label></td>'
                            }
                        }else {
                            html += '<tr name="tt'+b+'" style="display: none;"><td style="padding: 10px 10px;width: 180px;"><label style="cursor: pointer;"><input onchange="iseFfective(this,'+ls[j].id+')" name="cmenu" cid="'+ls[j].id+'" type="checkbox"/>'+ls[j].permissionName+'</label></td>';

                            html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="q'+ls[j].id+'" name="queryp" qmenuId="'+ls[j].id+'" type="checkbox" disabled="true"/>查看</label></td>'
                            html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="a'+ls[j].id+'" name="addp" amenuId="'+ls[j].id+'" type="checkbox" disabled="true"/>添加</label></td>'
                            html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="d'+ls[j].id+'" name="delp" dmenuId="'+ls[j].id+'" type="checkbox" disabled="true"/>删除</label></td>'
                            html += '<td style="padding: 10px 10px"><label style="cursor: pointer;"><input id="e'+ls[j].id+'" name="editp" emenuId="'+ls[j].id+'" type="checkbox" disabled="true"/>修改</label></td>'
                        }
                    }
                }
                html += "</tr></table>"
                $("#me").html(html);

            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }
        }

    });
}

//收起展开
function ck(b) {
    var obj = document.getElementsByName("tt"+b);
    for(var b in obj){
        if($(obj[b]).css("display") == "none"){
            $(obj[b]).css("display","block");
        }else {
            $(obj[b]).css("display","none");
        }
    }
}

//根据二级菜单是否选中判断增删改查是否可选
function iseFfective(obj,j) {
    if($(obj).is(":checked")){
        $("#q"+j).attr("disabled",false);
        $("#a"+j).attr("disabled",false);
        $("#d"+j).attr("disabled",false);
        $("#e"+j).attr("disabled",false);
    }else {
        $("#q"+j).prop("checked",false);
        $("#a"+j).prop("checked",false);
        $("#d"+j).prop("checked",false);
        $("#e"+j).prop("checked",false);
        $("#q"+j).attr("disabled",true);
        $("#a"+j).attr("disabled",true);
        $("#d"+j).attr("disabled",true);
        $("#e"+j).attr("disabled",true);
    }
}

//修改权限
function updatePermissions() {
    //一级菜单id
    var obj = document.getElementsByName("pmenu");
    var pids = "";
    var num = $(obj).length;
    for(var i =0;i < num;i++){
        if($(obj[i]).is(":checked")){
            pids += $(obj[i]).attr("pid")+",";
        }
    }
    //二级菜单id
    var cobj = document.getElementsByName("cmenu");
    var cids = "";
    for(var i=0;i<cobj.length;i++){
        if($(cobj[i]).is(":checked")){
            cids += $(cobj[i]).attr("cid")+",";
        }
    }
    //查询权限菜单id
    var qobj = document.getElementsByName("queryp");
    var qids = "";
    for(var i=0;i<qobj.length;i++){
        if($(qobj[i]).is(":checked")){
            qids += $(qobj[i]).attr("qmenuId")+",";
        }
    }
    //添加权限菜单id
    var aobj = document.getElementsByName("addp");
    var aids = "";
    for(var i=0;i<aobj.length;i++){
        if($(aobj[i]).is(":checked")){
            aids += $(aobj[i]).attr("amenuId")+",";
        }
    }
    //删除权限菜单id
    var dobj = document.getElementsByName("delp");
    var dids = "";
    for(var i=0;i<dobj.length;i++){
        if($(dobj[i]).is(":checked")){
            dids += $(dobj[i]).attr("dmenuId")+",";
        }
    }
    //修改权限菜单id
    var eobj = document.getElementsByName("editp");
    var eids = "";
    for(var i=0;i<eobj.length;i++){
        if($(eobj[i]).is(":checked")){
            eids += $(eobj[i]).attr("emenuId")+",";
        }
    }
    var params = {
        "menuId":sessionStorage.rmId,
        "type":"change",
        "pids":pids,
        "cids":cids,
        "qids":qids,
        "aids":aids,
        "dids":dids,
        "eids":eids,
        "roleId":sessionStorage.roleId
    };
    $.ajax({
        type:"post",
        url: "/srole/updatePermissions",
        data:params,
        dataType: "json",
        success : function(data){
            if(data.code == 1 && data.code != undefined){
                alert("分配成功");
                parent.layer.close(sessionStorage.getItem("preparationIndex"));
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }
        }

    });
}

